# Copyright (C) 2014  Cisco Systems
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
# - Neither the names of the copyright holders nor the names of their
#   contributors may be used to endorse or promote products derived from this
#   software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# makefile for generating pages similar to the CWMP index page
# http://www.broadband-forum.org/cwmp, containing either TRs, TRs + WTs or
# TRs + WTs + ILs (currently IL-181 only)

# the report tool
REPORT = ../Report_Tool/report.pl

# the publish tool
PUBLISH = ../Publish_Tool/publish.pl
PUBLISHFLAGS += --makevar REPORT="$(REPORT)"
PUBLISHFLAGS += --od148=$(CONFIG)
PUBLISHFLAGSNOWARN = --makevar REPORT_FLAGS="--nowarnreport"

# the publish tool's "mini make" tool
MINIMAKE = ../Publish_Tool/deps.pl

# the tee tool
TEE = tee

# this directory must be manually populated with cwmp.zip downloaded from
# http://www.broadband-forum.org/cwmp.zip (the publish tool will unzip it)
CWMPZIPDIR = cwmp-zip
CWMPZIP = $(CWMPZIPDIR)/cwmp.zip

# this directory contains WTs that are about to be published and therefore
# should be included in the cwmp-trs directory
# suggestion: make this be a real directory containing soft links to the
#             relevant WT directories
PUBDIR = Publish

# this directory contains WTs that should be included in the cwmp-wts
# directory and ILs that should be included in the cwmp-ils directory
# suggestion: make this be a soft link to a directory that contains the latest
#             versions of all relevant WTs and ILs
WTDIR = Standards

# these are the output directories for TRs, TRs + WTs and TRs + WTs + ILs
# respectively
TRWORKDIR = cwmp-trs
WTWORKDIR = cwmp-wts
ILWORKDIR = cwmp-ils

# this is the CWMP index file ready for use on the BBF web site
CWMPINDEX = cwmpindex.html

# these are the makefiles (generated by the publish tool) for TRs, TRs + WTs
# and TRs + WTs + ILs respectively
TRMAKEFILE = makefile-$(TRWORKDIR)
WTMAKEFILE = makefile-$(WTWORKDIR)
ILMAKEFILE = makefile-$(ILWORKDIR)

# the publish tool's config file
CONFIG = config.txt

# default target builds cwmp-trs and (if the Standards directory exist)
# cwmp-wts and cwmp-ils
ifeq "$(wildcard $(WTDIR))" ""
  ALL = $(TRWORKDIR)
else
  ALL = $(TRWORKDIR) $(WTWORKDIR) $(ILWORKDIR)
endif
all: $(ALL)

# cwmpindex target builds CWMP index file ready for use on the BBF web site
.PHONY: cwmpindex
cwmpindex: $(CWMPINDEX)

# TRs
.PHONY: $(TRWORKDIR)
$(TRWORKDIR): $(CWMPZIP) $(PUBDIR)
	$(PUBLISH) $(PUBLISHFLAGS) $(PUBLISHFLAGSNOWARN) \
		--cwmpdir=$(CWMPZIPDIR) \
		--wtdir=$(PUBDIR) \
		--workdir=$(TRWORKDIR) | \
	$(TEE) $(TRMAKEFILE) | $(MINIMAKE)

# TRs and WTs
.PHONY: $(WTWORKDIR)
$(WTWORKDIR): $(CWMPZIP) $(WTDIR)
	$(PUBLISH) $(PUBLISHFLAGS) --cwmpdir=$(CWMPZIPDIR) \
		--wtdir=$(WTDIR) \
		--workdir=$(WTWORKDIR) | \
	$(TEE) $(WTMAKEFILE) | $(MINIMAKE)

# TRs, WTs and ILs
.PHONY: $(ILWORKDIR)
$(ILWORKDIR): $(CWMPZIP) $(WTDIR)
	$(PUBLISH) $(PUBLISHFLAGS) --cwmpdir=$(CWMPZIPDIR) \
		--ildir=$(WTDIR) --wtdir=$(WTDIR) \
		--workdir=$(ILWORKDIR) | \
	$(TEE) $(ILMAKEFILE) | $(MINIMAKE)

# this differs from cwmp-trs/index.html in that it is ready to use on the BBF
# web site
# XXX unfortunately at present it is necessary manually to specify the inputs;
#     would like cwmp-trs/index.html to be directly usable
$(CWMPINDEX): $(TRWORKDIR)
	$(REPORT) --quiet \
        --include=$(TRWORKDIR) --report=htmlbbf --configfile=$(CONFIG) \
	cwmp-1-0.xsd cwmp-1-1.xsd cwmp-1-2.xsd cwmp-1-3.xsd cwmp-1-4.xsd \
	cwmp-datamodel-1-0.xsd cwmp-datamodel-1-1.xsd cwmp-datamodel-1-2.xsd \
	cwmp-datamodel-1-3.xsd cwmp-datamodel-1-4.xsd cwmp-datamodel-1-5.xsd \
	cwmp-datamodel-report.xsd \
	cwmp-devicetype-1-0.xsd cwmp-devicetype-1-1.xsd \
	cwmp-devicetype-1-2.xsd cwmp-devicetype-1-3.xsd \
	cwmp-devicetype-features.xsd \
	tr-232-1-0-0-serviceSpec.xsd \
        cwmp-UDPLightweightNotification-1-0.xsd \
        cwmp-xmppConnReq-1-0.xsd \
	tr-098-1-8.xml \
	tr-104-1-1.xml \
	tr-104-2-0.xml \
	tr-135-1-3.xml \
	tr-140-1-2.xml \
	tr-181-1-6.xml \
	tr-181-2-9.xml \
	tr-196-1-1.xml \
	tr-196-2-0.xml >$@

clean:
	$(RM) -rf $(TRMAKEFILE) $(WTMAKEFILE) $(ILMAKEFILE) $(CWMPINDEX)

distclean: clean
	$(RM) -rf $(TRWORKDIR) $(WTWORKDIR) $(ILWORKDIR)
